package nero.yncc.com.createart.custom;

import android.animation.AnimatorSet;
import android.animation.TypeEvaluator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.BounceInterpolator;
import android.widget.LinearLayout;
/**
 * Created by nero on 17-6-30.
 */

public class CustomNoTopBottomViewPager extends ViewPager {
    int screenHeight = 0; //屏幕高度
    int screenWidth = 0; //屏幕高度

    int nowHeight = 0;  //当前高度

    float nowScale = 0.0f; //当前拉的比例
    int bottomWidth = 0;   //底部宽度
    double bottomTranslate = 1.0f; //底部透明度
    double topTranslate = 1.0f; //顶部透明度

    float needHeightScale = 0.5f;//当下面滑动到屏幕高度的多少进行判断是还原还是上拉操作

//    MyGestureListener gestureListener;  //手势监听
    GestureDetector gestureDetector;    //手势托管


    private void init(){
//        //手势监听类
//        gestureListener = new MyGestureListener();
//        gestureDetector = new GestureDetector(gestureListener);

        setLongClickable(true);


        setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    Log.e("1312321","up");
                    //在这里判断是否有拉来超越限制 进行还原或者新建操作
                    ValueAnimator animator_height = null;
                    ValueAnimator animator_width = null;
                    ValueAnimator animator_alpha_top = null;
                    ValueAnimator animator_alpha_bottom = null;
                    if (nowHeight >= screenHeight * needHeightScale) {
                        //还原
                        animator_height = ValueAnimator.ofObject(new HeightEvalutor(), nowHeight, screenHeight);
                        animator_width = ValueAnimator.ofObject(new WeightEvalutor(), bottomWidth, 0);
                        animator_alpha_top = ValueAnimator.ofFloat((float) topTranslate, 1.0f);
                        animator_alpha_bottom = ValueAnimator.ofFloat((float) bottomTranslate, 0.0f);
                    } else {
                        //上拉
                        animator_height = ValueAnimator.ofObject(new HeightEvalutor(), nowHeight, 0);
                        animator_width = ValueAnimator.ofObject(new WeightEvalutor(), bottomWidth, screenWidth);
                        animator_alpha_top = ValueAnimator.ofFloat((float) topTranslate, 0.0f);
                        animator_alpha_bottom = ValueAnimator.ofFloat((float) bottomTranslate, 1.0f);
                    }
                    animator_alpha_top.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                        @Override
                        public void onAnimationUpdate(ValueAnimator animation) {
                            CustomNoTopBottomViewPager.this.setAlpha((float) animation.getAnimatedValue());
                        }
                    });

                    animator_alpha_bottom.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                        @Override
                        public void onAnimationUpdate(ValueAnimator animation) {
//                            viewBottom.setAlpha((float) animation.getAnimatedValue());
                        }
                    });

                    animator_height.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                        @Override
                        public void onAnimationUpdate(ValueAnimator animation) {
                            LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(-1,(int) animation.getAnimatedValue());
                            CustomNoTopBottomViewPager.this.setLayoutParams(params);
                        }
                    });
                    animator_width.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                        @Override
                        public void onAnimationUpdate(ValueAnimator animation) {
//                            viewBottom.setWidth((int) animation.getAnimatedValue());
                        }
                    });
                    //设置动画的弹跳差值器
                    animator_height.setInterpolator(new BounceInterpolator());
                    animator_height.setDuration(500);

                    animator_width.setInterpolator(new BounceInterpolator());
                    animator_width.setDuration(500);

                    AnimatorSet set = new AnimatorSet();
                    set.play(animator_height).with(animator_width);
                    set.start();
                    return true;
                } else {
                    return gestureDetector.onTouchEvent(event);
                }
            }
        });

    }

    /**
     * 高度估值器
     */
    private class HeightEvalutor implements TypeEvaluator<Integer> {

        @Override
        public Integer evaluate(float fraction, Integer startValue, Integer endValue) {

            return Float.valueOf((nowHeight + fraction * (endValue - startValue))).intValue();
        }
    }

    /**
     * 宽度估值器
     */
    private class WeightEvalutor implements TypeEvaluator<Integer> {

        @Override
        public Integer evaluate(float fraction, Integer startValue, Integer endValue) {

            return Float.valueOf((bottomWidth + fraction * (endValue - startValue))).intValue();
        }
    }


    public CustomNoTopBottomViewPager(Context context) {
        super(context);
        init();
    }

    public CustomNoTopBottomViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {

        return false;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        return false;
    }


}
